﻿using MediatR;
using MyMES.Domain.zmc;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Read.API.Application.Commends.Departments;

namespace MyMES.Read.API.Application.CommendHandlers.Departments
{

    /// <summary>
    ///  API请求处理命令
    /// </summary>
    public class GetDepartmentCommendHandlers : IRequestHandler<GetDepartmentCommends, APIResult<List<Department>>>
    {

        private readonly IBaseRepository<Department> depRepository;

        public GetDepartmentCommendHandlers(IBaseRepository<Department> depRepository)
        {
            this.depRepository = depRepository;
        }

        /// <summary>
        /// 获取部门列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<List<Department>>> Handle(GetDepartmentCommends request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<List<Department>> result = new APIResult<List<Department>>();
                result.Code = APIEnum.查询成功;
                result.Msg = "查询成功";
                string sql = "select * from department";
                var list = await depRepository.SelectAsync(sql);
                result.data = list;
                return result;
            }
            catch (Exception)
            {

                throw;
            }

        }
    }
}
